package com.itheima.health.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.PageBean;
import com.itheima.health.common.PageDto;
import com.itheima.health.exception.BizException;
import com.itheima.health.mapper.CheckGroupAndCheckItemMapper;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.model.dtos.AddCheckGroupDto;
import com.itheima.health.model.dtos.UpdateCheckGroupDto;
import com.itheima.health.model.entity.CheckGroup;
import com.itheima.health.model.vos.CheckGroupVo;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 这个类是：
 *
 * @author: 25122
 * @date: 2024/6/2 21:38
 * @version: 1.0
 */
@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupAndCheckItemMapper checkGroupAndCheckItemMapper;
    /**
     * 分页查找
     *
     * @param dto dto
     * @return {@code PageBean<CheckGroupVo> }
     */
    @Override
    public PageBean<CheckGroupVo> findPage(PageDto dto) {
        PageHelper.startPage(dto.getCurrentPage(),dto.getPageSize());
        Page<CheckGroupVo> page =  checkGroupMapper.findPage(dto.getQueryString());
        return new PageBean<>(page.getTotal(),page.getResult());
    }

    /**
     * 添加检查组
     *
     * @param dto
     * @param checkItemIds 检查项目id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCheckGroup(AddCheckGroupDto dto, Integer[] checkItemIds) {
        CheckGroup checkGroup = BeanUtil.copyProperties(dto, CheckGroup.class);
        checkGroupMapper.insert(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        // 判断一下 如果ids为空，就不添加了
        if (ids.length > 0) {
            checkGroupAndCheckItemMapper.insert(ids, checkGroupId);
        }
    }

    /**
     * 按id查找检查组
     *
     * @param id id
     * @return {@code CheckGroupVo }
     */
    @Override
    public CheckGroupVo findCheckGroupById(Integer id) {
        return checkGroupMapper.selectById(id);
    }

    /**
     * 更新检查组
     *
     * @param checkItemIds 检查项目id
     * @param dto          dto
     */
    @Override
    public void updateCheckGroup(Integer[] checkItemIds, UpdateCheckGroupDto dto) {
        CheckGroup checkGroup = BeanUtil.copyProperties(dto, CheckGroup.class);
        checkGroupMapper.updateCheckGroup(checkGroup);
        checkGroupAndCheckItemMapper.deleteByCheckGroupId(dto.getId());
        // 判断一下 如果checkItemIds为空，就不添加了
        if (checkItemIds.length > 0) {
            checkGroupAndCheckItemMapper.insert(checkItemIds, dto.getId());
        }
    }

    /**
     * 删除检查组
     *
     * @param id id
     */
    @Override
    public void deleteCheckGroup(Integer id) {
        List<Integer> list = checkGroupAndCheckItemMapper.selectByCheckGroupId(id);
        if (!list.isEmpty()){
            //如果不为空 说明有关联的检查项 不能删除
            throw new BizException("已关联检查项");
        }
        checkGroupMapper.deleteById(id);
    }
}
